using System;
using System.Collections.Generic;
using System.Text;
using Cores;
using System.Security.Cryptography;
using System.Data;
using System.Data.SqlClient;

namespace DAOLayer
{
    public class TaiKhoanDao
    {
        private Connection _conn;

        public Connection Conn
        {
            get { return _conn; }
            set { _conn = value; }
        }
        public TaiKhoanDao()
        {
            Conn = new Connection();
            if (Conn.Connect().State == ConnectionState.Closed)
            {
                Conn.ConnObj.Open();
            }
        }
        ~TaiKhoanDao()
        {
            return;
        }

        public int KTUser(string username, string password)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn.ConnObj;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select dbo.fLayMaKH(@UserName,@Password)";

            cmd.Parameters.AddWithValue("@UserName", username);
            cmd.Parameters.AddWithValue("@Password", password);
            SqlDataReader rd = cmd.ExecuteReader();
            int result = 0;
            if (rd.Read())
                result = rd.GetInt32(0);
            rd.Close();
            Conn.Disconnect();
            return result;
        }

        public static int KiemTraQuyenDangNhap(string userName)
        {
            TaiKhoanDao tk = new TaiKhoanDao();
            Connection conn = tk.Conn;
            if (conn.ConnObj.State == ConnectionState.Closed)
            {
                conn.ConnObj.Open();
            }
            SqlCommand cmd = new SqlCommand("spLayQuyenDangNhap", conn.ConnObj);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserName", userName);
            int res = 0;
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                res = rd.GetInt32(0);
            }
            rd.Close();
            conn.Disconnect();
            return res;
        }

        public static string Encrypte(string inputString)
        {
            UTF32Encoding u = new UTF32Encoding();
            byte[] bytes = u.GetBytes(inputString);
            MD5 md = new MD5CryptoServiceProvider();
            byte[] result = md.ComputeHash(bytes);
            return Convert.ToBase64String(result);
        }

        public int LayMaTaiKhoan(string username, string password)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn.ConnObj;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select dbo.fTimKiemTaiKhoanKH(@UserName,@Password)";

            cmd.Parameters.AddWithValue("@UserName", username);
            cmd.Parameters.AddWithValue("@Password", password);
            SqlDataReader rd = cmd.ExecuteReader();
            int result = 0;
            if (rd.Read())
                result = rd.GetInt32(0);
            rd.Close();
            Conn.Disconnect();
            return result;
        }
    }
}
